﻿package mortal.game.view.npc
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.npc.data.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.drama.operations.npctalk.*;

    public class NpcTaskAutoDialogModule extends Window
    {
        private var _bg2:GImageBitmap;
        private var _text:TaskDramaTalkMaskText;
        private var _txtName:GTextFiled;
        private var _taskInfo:TaskInfo;
        private var _npcInfo:TNpc;
        private var _datas:Array;
        private var _curData:NpcTaskAutoDialogData;
        private var _timerId:int = -1;
        private var _textRolling:Boolean = false;
        protected var _roleModel:RoleModelPlayer;
        protected var _img2d:Img2D;
        protected var _bgBitmapData:BitmapData;
        protected var _isResGot:Boolean = false;
        protected var _npc3d:Rect3DObject;
        protected var _effectPlayerArray:Vector.<EffectPlayer>;
        protected var _bodyPlayer:ActionPlayer;
        protected var _hangPlayer:ActionPlayer;
        protected var _rect3DMask:Rect3DMask;

        public function NpcTaskAutoDialogModule()
        {
            this._datas = [];
            this._effectPlayerArray = new Vector.<EffectPlayer>;
            this.layer = LayerManager.npcDialogLayr;
            return;
        }// end function

        public function get taskInfo() : TaskInfo
        {
            return this._taskInfo;
        }// end function

        public function showTaskTalk(param1:Array) : void
        {
            this._datas = this._datas.concat(param1);
            this.showNext();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super._isAddClolseButton = false;
            super.createDisposedChildrenImpl();
            this._bg2 = UIFactory.gImageBitmap("", 18, 78, this.contentTopOf3DSprite);
            LoaderHelp.setBitmapdata(ImagesConst.TaskAutoTalkBg2 + ".swf", this._bg2);
            this._txtName = UIFactory.gTextField("", this._bg2.x - 11, this._bg2.y + 5, 120, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatBai.center());
            this._text = UICompomentPool.getUICompoment(TaskDramaTalkMaskText) as TaskDramaTalkMaskText;
            this._text.x = 110;
            this._text.y = 40;
            this._text.mouseChildren = false;
            this._text.mouseEnabled = false;
            this._text.resize(405);
            this.contentTopOf3DSprite.addChild(this._text);
            LoaderManager.instance.load(ImagesConst.TaskAutoTalkBg + ".swf", this.loaded);
            return;
        }// end function

        private function showNext() : void
        {
            if (this._textRolling)
            {
                return;
            }
            if (this._curData != null)
            {
                this._curData = null;
            }
            if (this._datas == null || this._datas.length == 0)
            {
                this.hide();
                return;
            }
            this._textRolling = true;
            this._curData = this._datas.shift();
            this._npcInfo = this._curData.npcInfo;
            this._text.show(this.getNpcTalkData(this._curData), this.showNextEnd);
            this.updateNpcInfo(this._curData.npcInfo);
            return;
        }// end function

        public function updateNpcInfo(param1:TNpc) : void
        {
            this._npcInfo = param1;
            if (!this._isResGot)
            {
                return;
            }
            this.dispose3d();
            this._img2d = new Img2D(null, this._bgBitmapData, new Rectangle(0, 0, this._bgBitmapData.width, this._bgBitmapData.height), true);
            this._npc3d = Rect3DManager.instance.getRect3dByWindow(this);
            if (!this._npc3d)
            {
                this._npc3d = Rect3DManager.instance.registerWindow(false, new Rectangle(0, 0, this._bgBitmapData.width, this._bgBitmapData.height - 30), this);
            }
            this._rect3DMask = this._npc3d.createRect3dMask(new Rectangle(-100, -100, 208, 185));
            Rect3DManager.instance.windowShowHander(null, this);
            this._npc3d.addImg(this._img2d);
            if (this._npcInfo != null)
            {
                this._txtName.text = this._npcInfo.name;
                this.updateToCurNpc();
            }
            else
            {
                this._txtName.text = Cache.instance.role.roleEntityInfo.entityInfo.name;
                this.addSelf();
            }
            return;
        }// end function

        private function showNextEnd() : void
        {
            this._timerId = setTimeout(this.timeout, 80);
            return;
        }// end function

        private function timeout() : void
        {
            this._timerId = -1;
            this._textRolling = false;
            this.showNext();
            return;
        }// end function

        private function getNpcTalkData(param1:NpcTaskAutoDialogData) : TaskDramaTalkData
        {
            var _loc_2:* = new TaskDramaTalkData();
            _loc_2.popupTime = 0;
            _loc_2.rowWidth = 405;
            _loc_2.speed = 16;
            _loc_2.talk = param1.talk;
            _loc_2.showTime = param1.showSeconds * 1000 + _loc_2.talk.length / _loc_2.speed * 1000;
            _loc_2.talkFontLeading = 2;
            _loc_2.talkFontSize = 14;
            return _loc_2;
        }// end function

        private function loaded(param1:SWFInfo) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._bgBitmapData = param1.bitmapData;
            this._isResGot = true;
            if (this._curData == null)
            {
                return;
            }
            this.updateNpcInfo(this._curData.npcInfo);
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._effectPlayerArray = new Vector.<EffectPlayer>;
            this._bg2.dispose(param1);
            this._bg2 = null;
            this._text.dispose(param1);
            this._text = null;
            this._txtName.dispose();
            this._txtName = null;
            this._taskInfo = null;
            this._npcInfo = null;
            this._curData = null;
            this._isResGot = false;
            this._datas = [];
            if (this._timerId > 0)
            {
                clearTimeout(this._timerId);
                this._timerId = -1;
            }
            this._textRolling = false;
            this._bgBitmapData = null;
            this.dispose3d();
            return;
        }// end function

        private function dispose3d() : void
        {
            if (this._npc3d)
            {
                if (this._img2d != null)
                {
                    this._npc3d.removeImg(this._img2d);
                    this._img2d.dispose(false);
                    this._img2d = null;
                }
                Rect3DManager.instance.disposeRect3d(this._npc3d);
                this._npc3d = null;
            }
            if (this._hangPlayer)
            {
                this._bodyPlayer.unHang(this._hangPlayer);
                this._hangPlayer.dispose();
                this._hangPlayer = null;
            }
            if (this._bodyPlayer != null)
            {
                this._effectPlayerArray.length = 0;
                this._bodyPlayer = null;
            }
            if (this._roleModel)
            {
                this._roleModel.dispose();
                this._roleModel = null;
            }
            return;
        }// end function

        override public function stageResize() : void
        {
            this.x = (Global.stage.stageWidth - 590) / 2;
            var _loc_1:* = GameController.shortcut.shortcutBar.height;
            if (!GameController.shortcut.shortcutBar.isTopLineShowing())
            {
                _loc_1 = _loc_1 - 42;
            }
            var _loc_2:* = Global.stage.stageHeight - _loc_1 - 120;
            var _loc_3:* = Global.stage.stageHeight / 2 + 170 + 10;
            this.y = Math.min(_loc_2, _loc_3);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.stageResize();
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            return;
        }// end function

        override public function set x(param1:Number) : void
        {
            super.x = param1;
            this.dispatchEvent(new WindowEvent(WindowEvent.POSITIONCHANGE));
            return;
        }// end function

        protected function updateToCurNpc() : void
        {
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:EffectPlayer = null;
            var _loc_8:Array = null;
            if (this._bodyPlayer == null)
            {
                this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
            }
            if (this._bodyPlayer.meshUrl != this._npcInfo.mesh && this._bodyPlayer.animUrl != this._npcInfo.bone)
            {
                this._bodyPlayer.load(this._npcInfo.mesh, this._npcInfo.bone, this._npcInfo.texture, this._npc3d.renderList);
                if (this._npcInfo.hangModel)
                {
                    _loc_4 = this._npcInfo.hangModel.split("#");
                    if (_loc_4.length >= 4)
                    {
                        if (!this._hangPlayer)
                        {
                            this._hangPlayer = FrObjectPool.getObject(ActionPlayer, this);
                            this._hangPlayer.changeAction(ActionName.Stand);
                            this._hangPlayer.selectEnabled = true;
                            this._hangPlayer.timerContorler = TimeControler.defaultTimeControler;
                            this._hangPlayer.play();
                        }
                        this._hangPlayer.load(_loc_4[0], _loc_4[1], _loc_4[2], this._npc3d.renderList);
                        this._hangPlayer.hangBoneName = _loc_4[3];
                        this._bodyPlayer.hang(this._hangPlayer);
                    }
                }
                if (this._npcInfo.selfEffect)
                {
                    _loc_2 = [];
                    _loc_3 = [];
                    _loc_4 = this._npcInfo.selfEffect.split("&");
                    _loc_5 = 0;
                    while (_loc_5 < _loc_4.length)
                    {
                        
                        _loc_6 = String(_loc_4[_loc_5]).split("#");
                        _loc_2.push(_loc_6[0]);
                        _loc_3.push(_loc_6[1]);
                        _loc_5++;
                    }
                    _loc_5 = 0;
                    while (_loc_5 < _loc_2.length)
                    {
                        
                        if (this._effectPlayerArray.length > _loc_5)
                        {
                            _loc_7 = this._effectPlayerArray[_loc_5];
                            this._bodyPlayer.unHang(_loc_7);
                            _loc_7.dispose();
                        }
                        _loc_7 = EffectPlayerPool.instance.getEffectPlayer(_loc_2[_loc_5], null, this._npc3d.renderList, false);
                        this._effectPlayerArray[_loc_5] = _loc_7;
                        _loc_7.hangBoneName = _loc_3[_loc_5];
                        this._bodyPlayer.hang(_loc_7);
                        _loc_7.play(PlayMode.ANIMATION_LOOP_MODE);
                        _loc_5++;
                    }
                }
            }
            this._bodyPlayer.changeAction(ActionName.Stand);
            this._bodyPlayer.play();
            var _loc_1:* = GameDefConfig.instance.getNpcMaxScale(this._npcInfo.code);
            if (_loc_1 != null)
            {
                _loc_8 = _loc_1.text.split(",");
                this._bodyPlayer.scaleValue = parseFloat(_loc_8[2]) / 2;
                this._npc3d.addObject3d(this._bodyPlayer, 58 + int(_loc_8[0]), 144 + int(_loc_8[1]), true, this._rect3DMask);
            }
            else
            {
                this._bodyPlayer.scaleValue = 1.3;
                this._npc3d.addObject3d(this._bodyPlayer, 58, 144, true, this._rect3DMask);
            }
            if (this._roleModel && this._roleModel.bodyPlayer)
            {
                this._npc3d.disposeObj3d(this._roleModel.bodyPlayer);
            }
            return;
        }// end function

        protected function addSelf() : void
        {
            if (this._roleModel == null)
            {
                this._roleModel = FrObjectPool.getObject(RoleModelPlayer, this);
                this._roleModel.entityInfo = Cache.instance.role.roleEntityInfo;
                this._roleModel.scaleAll = 1.3;
                this._roleModel.setRenderList(this._npc3d.renderList);
            }
            this._npc3d.addObject3d(this._roleModel, 58, 144, true, this._rect3DMask);
            if (this._bodyPlayer)
            {
                this._npc3d.disposeObj3d(this._bodyPlayer);
                this._bodyPlayer = null;
            }
            return;
        }// end function

    }
}
